<?php

/**
 * ajax actions.
 *
 * @package    shop
 * @subpackage ajax
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class ajaxActions extends sfActionsEx {

    public function executeUnregisterNewsletter(sfRequest $request) {
        $json = new stdClass();
        if ($request->getParameter('newsletter_email')) {
            $newsletter = NewsletterTable::getInstance()->findOneBy('email', $request->getParameter('newsletter_email'))->getFirst();
            if ($newsletter && $newsletter->getActive()) {
                $newsletter->setActive(false);
                $newsletter->save();
                $json->message = 'Podany email został usunięty z bazy newslettera.';
                $json->status = 1;
            } else {
                $json->message = 'Nie znaleziono podanego maila w bazie newslettera.';
                $json->status = 0;
            }
        } else {
            $json->message = 'Nie podano zadnego maila';
            $json->status = 0;
        }
        echo json_encode($json);
        die;
    }

    public function executeRegisterNewsletter(sfRequest $request) {
        $json = new stdClass();
        if ($request->getParameter('newsletter_email')) {
            $newsletter = NewsletterTable::getInstance()->findOneBy('email', $request->getParameter('newsletter_email'));
            if (!$newsletter) {
                $newsletter = new Newsletter();
                $newsletter->setEmail($request->getParameter('newsletter_email'));
            }
            if ($newsletter) {
                if ($newsletter->getActive()) {
                    $json->status = 0;
                    $json->message = 'Jesteś już zapisany w newsletterze';
                } else {
                    $json->status = 1;
                    $json->message = 'Zostałe dopisany do newslettera';
                    $newsletter->setActive(true);
                    $newsletter->save();
                }
            }
            echo json_encode($json);
            die;
        }
        return sfView::NONE;
    }

    public function executeSearch($request) {
        $json = array();
        echo json_encode($json);
        die;
    }

    public function executePages(sfRequest $request) {
        $link = $request->getParameter('route');
        $json = new stdClass();
        $json->status = 0;
        if ($link != NULL) {
            $json->link = $link;
            $page = PagesTable::getInstance()->findOneBy('route', $link);
            if ($page) {

                $this->page = $page;
                //$this->attributes = $page->getPagesAttributes();
                $json->status = 1;
                $json->content = $this->getPresentation();
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeLogin(sfRequest $request) {
        $post = $this->prepareRequest($request, array('login', 'password'));
        $json = new stdClass();
        $json->status = 0;
        $json->messageType = 'warning';
        if ($post->status === 1) {
            $json->message = 'Nie wypełniono wszystkich pól';
        } else if ($post->status === 2) {
            if (Authorization::getInstance()->create($post->values["login"], $post->values["password"])) {
                $json->status = 1;
                $json->mesageType = 'success';
                $json->message = 'Zalogowano poprawnie.';
            } else {
                $json->message = 'Nie znaleziono podanego użytkownika.';
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeRegisterAccount(sfRequest $request) {
        $post = $this->prepareRequest($request, array("account_name", "password", "re_password", "email", "name", "surname", "adress", "telephone", "birthday"));
        $json = new stdClass();
        $json->status = 0;
        $json->messageType = 'warning';
        if ($post->status === 1) {
            $json->message = 'Nie wypełniono wszystkich pól';
        } else if ($post->status === 2) {
            if (UserTable::getInstance()->findOneBy('name', $post->values['account_name'])) {
                $json->message = 'Podana nazwa użytkownika istnieje w naszej bazie danych';
            } else {
                if ($post->values['password'] == $post->values['re_password']) {
                    $json->message = 'Konto zostało utworzone poprawnie';
                    $account = new User();
                    $account->setName($post->values['account_name'])
                            ->setPassword($post->values['password'])
                            ->setEmail($post->value['email'])
                            ->save();
                    $data = new DeliveryData();
                    $data->setName($post->values['name'])
                            ->setSurname($post->values['surname'])
                            ->setAdress($post->values['adress'])
                            ->setTelephone($post->values['telephone'])
                            ->setBirthday($post->values['birthday'])
                            ->setUserId($account->getId())
                            ->save();
                }
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeLogout(sfRequest $request) {
        $json = new stdClass();
        $json->status = 0;
        $json->message = 'Nie mozna wylogowac';
        $json->messageType = 'warning';
        if (Authorization::getInstance()->isExists()) {
            Authorization::getInstance()->delete();
            $json->status = 1;
            $json->message = 'Wylogowano';
            $json->messageType = 'success';
        }
        echo json_encode($json);
        die;
    }

    public function executeContact(sfRequest $request) {
        $this->page = PagesTable::getInstance()->findOneBy('route', 'kontakt');
        $post = $this->prepareRequest($request, array('content', 'from', 'contact'));
        $json = new stdClass();
        $json->message = '';
        $json->status = 0;
        $json->content = $this->getPresentation();
        if ($post->status === 1) {
            $json->message = 'Nie wypelniono wszystkich pól.';
        } else if ($post->status === 2) {
            $json->status = 1;
            $json->message = 'Wiadomość wyslana poprawnie';
            $mail = new Mail();
            $mail->setSubject($post->values["subject"]);
            $mail->setTo('tairens@gmail.com');
            $mail->setTemplate('contact.html');
            $mail->addToVarHolder('name', $post->values["name"]);
            $mail->addToVarHolder('content', $post->values["content"]);
            $mail->send();
        }
        echo json_encode($json);
        die;
    }

    public function executeAddproduct(sfRequest $request) {
        $post = $this->prepareRequest($request, array('productid', 'count'));
        $json = new stdClass();
        $json->message = '';
        $json->status = 0;
        if ($post->status === 1) {
            $json->message = 'Nie wypelniono wszystkich pól.';
        } 
        else if ($post->status === 2) {
            $json->status = 1;
            $json->message = 'Produkt dodano do koszyka';
            myBucket::getInstance()->addNewObject($post->values['productid'], $post->values['count']);
            myBucket::getInstance()->save();
            $json->bucket = myBucket::getInstance()->getObjects();
        }
        echo json_encode($json);
        die;
    }

    public function executeOrder($request) {
        $json = new stdClass();
        $json->message = 'Nie znaleziono podanego typu transportu';
        $json->status = 0;
        if ($request->getParameter('delivery') != NULL) {
            $auth = Authorization::getInstance();
            if ($auth->isExists()) {
                try {
                    $order = new myOrder($auth->getUser(), $request->getParameter('delivery'));
                    $order->clean();
                    $json->status = 1;
                    $json->message = 'Zrealizowano nowe zamówienie!';
                } catch (Exception $object) {
                    $json->message = $object->getMessage();
                }
            } else {
                $json->message = 'Tylko zalogowani użytkownicy mogą dokonać tej czynności.';
            }
        }
        echo json_encode($json);
        die;
    }

    public function executeAddProductCount(sfRequest $request) {
        $json = new stdClass();
        $json->message = 'Wystąpił błąd podczas zmiany ilości zamówionych produktów.';
        $json->status = 0;
            if(myBucket::getInstance()->addObjectCount($request->getParameter('productid'), $request->getParameter('count'))){
                myBucket::getInstance()->save();
                $json->bucketValue = myBucket::getInstance()->getValue();
                $json->status = 1;
            }
        echo json_encode($json);
        die;
    }
    
    public function executeRemoveProduct(sfRequest $request) {
        $json = new stdClass();
        $json->message = 'Wystąpił błąd podczas zmiany ilości zamówionych produktów.';
        $json->status = 0;
            if(myBucket::getInstance()->removeObject($request->getParameter('productid'))){
                myBucket::getInstance()->save();
                $json->bucketValue = myBucket::getInstance()->getValue();
                $json->status = 1;
                $json->refresh = 0;
                if(myBucket::getInstance()->count() == 0){
                    $json->refresh = 1;
                }
            }
  
        echo json_encode($json);
        die;
    }

}
